#include "cgui.h"

//#include "CPlayerGUI.h"
void cGUI::Init(void)
{
	m_bUpdated = false;
	/********************** TEST **********************/
	CPlayerGUI::isetCurrentMana(0);
	CPlayerGUI::isetCurrentHealth(0);
	/********************** /TEST *********************/

	m_env = m_idDevice->getGUIEnvironment();
	m_oTextureMana     = m_oDriver->getTexture("../media/manabar.bmp");
	m_oTextureHealth   = m_oDriver->getTexture("../media/healthbar.bmp");
	m_oTextureMainBars = m_oDriver->getTexture("../media/barras.bmp");
	m_oMarco           = m_oDriver->getTexture("../media/marco.bmp");
	//m_oCurbas2         = m_oDriver->getTexture("../media/curbas2.bmp");

	m_oBarrita1        = m_oDriver->getTexture("../media/linea.bmp");
	m_oBarrita2        = m_oDriver->getTexture("../media/linea.bmp");

	m_oDriver->makeColorKeyTexture(m_oTextureMana, SColor(255, 255, 0, 255));
	m_oDriver->makeColorKeyTexture(m_oTextureHealth, SColor(255, 255, 0, 255));
	m_oDriver->makeColorKeyTexture(m_oTextureMainBars, SColor(255, 255, 0, 255));
	m_oDriver->makeColorKeyTexture(m_oMarco, SColor(255, 255, 0, 255));
	//m_oDriver->makeColorKeyTexture(m_oCurbas2, SColor(255, 255, 0, 255));

	if (m_oTextureMainBars)
	{
		m_MainBar = m_env->addImage(
										m_oTextureMainBars,
										position2d<s32>(117,530)
									 );

		m_ManaBar = m_env->addImage(
										m_oTextureMana,
										position2d<s32>(116,547)
								   );

		m_HealthBar = m_env->addImage(
										m_oTextureHealth,
										position2d<s32>(428,547)
									 );
		m_nMana = 0;
		m_nHealth = 0;
		if (m_bUsePercentages == true)
		{
			setManaPercentage(m_nMana);
			setHealthPercentage(m_nHealth);
		}
		else
		{
			setMana(m_nMana);
			setHealth(m_nHealth);
		}

		m_Marco = m_env->addImage(
										m_oMarco,
										position2d<s32>(112,542)
								   );


   }
}

void cGUI::DisplayData(void)
{
}

void cGUI::Draw(void)
{
	m_env->drawAll();
}

void cGUI::setHealth(int nHealth)
{
	if (nHealth < 0)
	{
		nHealth = 0;
		CPlayerGUI::isetCurrentHealth(0);
	}
	else if (nHealth > 0)
	{
		video::ITexture* tex=BlizStretchTexture(m_idDevice,"BlizTexture",m_oBarrita2,core::dimension2d<s32>(1+nHealth,32));
		m_env->getVideoDriver()->draw2DImage(tex, position2d<s32>(117,546));
	}
	//for (int i = 0; i < nHealth; i++)
	//	m_env->addImage(m_oBarrita,position2d<s32>(108+i,544));
		//m_env->getVideoDriver()->draw2DImage(m_oBarrita, position2d<s32>(108+i,544));
}

void cGUI::setMana(int nMana)
{
	if (nMana < 0)
	{
		nMana = 0;
		CPlayerGUI::isetCurrentMana(0);
	}
	else if (nMana > 0)
	{
		video::ITexture* tex=BlizStretchTexture(m_idDevice,"BlizTexture",m_oBarrita2,core::dimension2d<s32>(nMana + 1,32));
		m_env->getVideoDriver()->draw2DImage(tex, position2d<s32>(682-nMana,546));
	}
}

void cGUI::setHealthPercentage(int nPercentage)
{
	int nCant = (int)((nPercentage * 256) / 100);
	setHealth(nCant);
}

void cGUI::setManaPercentage(int nPercentage)
{
	int nCant = (int)((nPercentage * 256) / 100);
	setMana(nCant);
}

void cGUI::SetHealth(int nHealth)
{
	m_bUpdated = true;
	m_nHealth = nHealth;
	setHealth(nHealth);
}

void cGUI::SetMana(int nMana)
{
	m_bUpdated = true;
	m_nMana = nMana;
	setMana(nMana);
}
